package com.hm.service.impl.task;

import com.hm.service.entity.cron.CronExpressionBean;
import com.hm.service.framework.base.BaseServiceImpl;
import com.hm.service.mapper.cron.CronExpressionMapper;
import com.hm.service.service.main.CronExpressionService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class CronExpressionImpl extends BaseServiceImpl implements CronExpressionService {

    @Qualifier("MainSqlSessionFactory")
    public final SqlSessionFactory sqlSessionFactory;

    public CronExpressionImpl(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    @Override
    public String getCronExpression(String cronExpressionId) {
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            CronExpressionMapper mapper = session.getMapper(CronExpressionMapper.class);
            HashMap<String, String> params = new HashMap<>();
            params.put("cronExpressionId", cronExpressionId);
            CronExpressionBean bean = mapper.getCronExpression(params);
            return bean.cronExpression;
        }
    }

    @Override
    public List<CronExpressionBean> getAllExpression() {
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            CronExpressionMapper mapper = session.getMapper(CronExpressionMapper.class);
            return mapper.queryAllExpression();
        }
    }

    @Override
    public String createCronExpression(String cronExpressionId, String cronExpression, String description) {
        //参数校验
        if (cronExpressionId == null || cronExpressionId.isEmpty() || cronExpression == null || cronExpression.isEmpty() || description == null || description.isEmpty()) {
            return generateFail("参数异常");
        }
        //判断id是否整数
        boolean integer = isInteger(cronExpressionId);
        if (!integer) {
            return generateFail("cronExpressionId必须为整数");
        }
        int intCron = Integer.parseInt(cronExpressionId);
        if (intCron < 100 || intCron > 999) {
            return generateFail("cronExpressionId异常");
        }
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            CronExpressionMapper mapper = session.getMapper(CronExpressionMapper.class);
            HashMap<String, String> params1 = new HashMap<>();
            params1.put("cronExpressionId", cronExpressionId);
            CronExpressionBean bean = mapper.getCronExpression(params1);
            if (bean != null) {
                //说明有重复id
                return generateFail("该ID已存在");
            }
            HashMap<String, String> params = new HashMap<>();
            params.put("id", cronExpressionId);
            params.put("cronExpression", cronExpression);
            params.put("description", description);
            mapper.createCronExpression(params);
            return generateSucceed("创建成功");
        }
    }

    private static boolean isInteger(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}
